package com.atguigu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.myhandler.CustomBlockerHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @USER: dayu
 * @DATE: 2020/4/16
 * @DESCRIPTION:
 */
@RestController
public class RateLimitController {

    /**
     * 根据资源名限流
     * @return
     */
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handlerException")
    public CommonResult byResource(){
        return new CommonResult(200,"按资源名称测试OK  \t O(∩_∩)O",new Payment(20200416l,"serial0001"));
    }
    public CommonResult handlerException(BlockException blockException){
        return new CommonResult(444,blockException.getClass().getCanonicalName()+"\t服务不可用\t o(╥﹏╥)o");
    }


    /**
     * 根据url限流
     * @return
     */
    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl(){
        return new CommonResult(200,"按url测试OK  \t O(∩_∩)O",new Payment(20200412l,"serial0002"));
    }


    /**
     * 根据自定义方法限流
     * @return
     */
    @GetMapping("/customBlockerHandler")
    @SentinelResource(value = "customBlockerHandler",
            blockHandlerClass = CustomBlockerHandler.class,
            blockHandler = "handlerException001")
    public CommonResult customBlockerHandler(){
        return new CommonResult(200,"根据自定义方法限流  \t O(∩_∩)O",new Payment(2020l,"serial0003"));
    }
}
